home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / RCSGEN.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  12KB  |  384 lines

  1. /*
  2.  *                     RCS revision generation
  3.  */
  4.  
  5. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  6.    Copyright 1990 by Paul Eggert
  7.    Distributed under license by the Free Software Foundation, Inc.
  8.  
  9. This file is part of RCS.
  10.  
  11. RCS is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 1, or (at your option)
  14. any later version.
  15.  
  16. RCS is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with RCS; see the file COPYING.  If not, write to
  23. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. Report problems and direct all questions to:
  26.  
  27.     rcs-bugs@cs.purdue.edu
  28.  
  29. */
  30.  
  31.  
  32.  
  33. /* $Log: rcsgen.c%v $
  34.  * Revision 1.2  1991/08/23  13:34:38  SGP
  35.  * Ported to MSDOS using Borland C++
  36.  *
  37.  * Revision 5.6  1990/12/27  19:54:26  eggert
  38.  * Fix bug: rcs -t inserted \n, making RCS file grow.
  39.  *
  40.  * Revision 5.5  1990/12/04  05:18:45  eggert
  41.  * Use -I for prompts and -q for diagnostics.
  42.  *
  43.  * Revision 5.4  1990/11/01  05:03:47  eggert
  44.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  45.  *
  46.  * Revision 5.3  1990/09/21  06:12:43  hammer
  47.  * made putdesc() treat stdin the same whether or not it was from a terminal
  48.  * by making it recognize that a single '.' was then end of the
  49.  * description always
  50.  *
  51.  * Revision 5.2  1990/09/04  08:02:25  eggert
  52.  * Fix `co -p1.1 -ko' bug.  Standardize yes-or-no procedure.
  53.  *
  54.  * Revision 5.1  1990/08/29  07:14:01  eggert
  55.  * Clean old log messages too.
  56.  *
  57.  * Revision 5.0  1990/08/22  08:12:52  eggert
  58.  * Remove compile-time limits; use malloc instead.
  59.  * Ansify and Posixate.
  60.  *
  61.  * Revision 4.7  89/05/01  15:12:49  narten
  62.  * changed copyright header to reflect current distribution rules
  63.  * 
  64.  * Revision 4.6  88/08/28  14:59:10  eggert
  65.  * Shrink stdio code size; allow cc -R; remove lint; isatty() -> ttystdin()
  66.  * 
  67.  * Revision 4.5  87/12/18  11:43:25  narten
  68.  * additional lint cleanups, and a bug fix from the 4.3BSD version that
  69.  * keeps "ci" from sticking a '\377' into the description if you run it
  70.  * with a zero-length file as the description. (Guy Harris)
  71.  * 
  72.  * Revision 4.4  87/10/18  10:35:10  narten
  73.  * Updating version numbers. Changes relative to 1.1 actually relative to
  74.  * 4.2
  75.  * 
  76.  * Revision 1.3  87/09/24  13:59:51  narten
  77.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  78.  * warnings)
  79.  * 
  80.  * Revision 1.2  87/03/27  14:22:27  jenkins
  81.  * Port to suns
  82.  * 
  83.  * Revision 4.2  83/12/02  23:01:39  wft
  84.  * merged 4.1 and 3.3.1.1 (clearerr(stdin)).
  85.  * 
  86.  * Revision 4.1  83/05/10  16:03:33  wft
  87.  * Changed putamin() to abort if trying to reread redirected stdin.
  88.  * Fixed getdesc() to output a prompt on initial newline.
  89.  * 
  90.  * Revision 3.3.1.1  83/10/19  04:21:51  lepreau
  91.  * Added clearerr(stdin) for re-reading description from stdin.
  92.  * 
  93.  * Revision 3.3  82/11/28  21:36:49  wft
  94.  * 4.2 prerelease
  95.  * 
  96.  * Revision 3.3  82/11/28  21:36:49  wft
  97.  * Replaced ferror() followed by fclose() with ffclose().
  98.  * Putdesc() now suppresses the prompts if stdin
  99.  * is not a terminal. A pointer to the current log message is now
  100.  * inserted into the corresponding delta, rather than leaving it in a
  101.  * global variable.
  102.  *
  103.  * Revision 3.2  82/10/18  21:11:26  wft
  104.  * I added checks for write errors during editing, and improved
  105.  * the prompt on putdesc().
  106.  *
  107.  * Revision 3.1  82/10/13  15:55:09  wft
  108.  * corrected type of variables assigned to by getc (char --> int)
  109.  */
  110.  
  111.  
  112.  
  113.  
  114. #include "rcsbase.h"
  115.  
  116. libId(genId, "$Id: rcsgen.c%v 1.2 1991/08/23 13:34:38 SGP Exp $")
  117.  
  118. int interactiveflag;  /* Should we act as if stdin is a tty?  */
  119. struct cbuf curlogmsg;  /* buffer for current log message */
  120. struct buf curlogbuf;  /* same, including allocated but unused bytes */
  121.  
  122. enum stringwork {copy, edit, expand, edit_expand };
  123.  
  124. static void scandeltatext P((struct hshentry*,enum stringwork));
  125.  
  126.  
  127.  
  128.  
  129.     const char *
  130. buildrevision(const struct hshentries *deltas,
  131.               struct hshentry *target,
  132.               int tostdout,
  133.               int expandflag)
  134. /* Function: Generates the revision given by target
  135.  * by retrieving all deltas given by parameter deltas and combining them.
  136.  * If tostdout is set, the revision is printed on the standard output,
  137.  * otherwise written into a temporary file.
  138.  * Temporary files are put into tmp unless the caller has already set up
  139.  * the temp file directory by invoking initeditfiles(), which sets fcopy.
  140.  * if expandflag is set, keyword expansion is performed.
  141.  * Returns nil on errors, the name of the file with the revision otherwise.
  142.  *
  143.  * Algorithm: Copy initial revision unchanged.  Then edit all revisions but
  144.  * the last one into it, alternating input and output files (resultfile and
  145.  * editfile). The last revision is then edited in, performing simultaneous
  146.  * keyword substitution (this saves one extra pass).
  147.  * All this simplifies if only one revision needs to be generated,
  148.  * or no keyword expansion is necessary, or if output goes to stdout.
  149.  */
  150. {
  151.     static const char nonnil[] = ""; /* some char* value that isn't nil */
  152.  
  153.     if (deltas->first == target) {
  154.                 /* only latest revision to generate */
  155.         if (tostdout) {
  156.                         fcopy=stdout;
  157.             scandeltatext(target, expandflag?expand:copy);
  158.             return nonnil;
  159.                 } else {
  160.             if (!fcopy)
  161.                 inittmpeditfiles();
  162.                         scandeltatext(target,expandflag?expand:copy);
  163.                         ffclose(fcopy);
  164.                         return(resultfile);
  165.                 }
  166.         } else {
  167.                 /* several revisions to generate */
  168.         if (!fcopy)
  169.             inittmpeditfiles();
  170.                 /* write initial revision into fcopy, no keyword expansion */
  171.         scandeltatext(deltas->first, copy);
  172.         while ((deltas=deltas->rest)->rest) {
  173.                         /* do all deltas except last one */
  174.             scandeltatext(deltas->first, edit);
  175.                 }
  176.         if (expandflag | tostdout) {
  177.                         /* first, get to beginning of file*/
  178.             finishedit((struct hshentry *)nil);
  179.             swapeditfiles(tostdout);
  180.                 }
  181.         scandeltatext(deltas->first, expandflag ? edit_expand : edit);
  182.         finishedit(expandflag ? deltas->first : (struct hshentry*)nil);
  183.         if (tostdout)
  184.             return nonnil;
  185.         ffclose(fcopy);
  186.         return resultfile;
  187.         }
  188. }
  189.  
  190.  
  191.  
  192.     static void
  193. scandeltatext(struct hshentry * delta, enum stringwork func)
  194. /* Function: Scans delta text nodes up to and including the one given
  195.  * by delta. For the one given by delta, the log message is saved into
  196.  * curlogmsg and the text is processed according to parameter func.
  197.  * Assumes the initial lexeme must be read in first.
  198.  * Does not advance nexttok after it is finished.
  199.  */
  200. {
  201.     const struct hshentry *nextdelta;
  202.     struct cbuf cb;
  203.  
  204.         for (;;) {
  205.                 nextlex();
  206.                 if (!(nextdelta=getnum())) {
  207.             fatserror("can't find delta for revision %s", delta->num);
  208.                 }
  209.         getkeystring(Klog);
  210.         if (delta==nextdelta) {
  211.             cb = savestring(&curlogbuf);
  212.             delta->log = curlogmsg =
  213.                 cleanlogmsg(curlogbuf.string, cb.size);
  214.                 } else {readstring();
  215.                 }
  216.                 nextlex();
  217.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  218.             ignorephrase();
  219.         getkeystring(Ktext);
  220.  
  221.         if (delta==nextdelta)
  222.             break;
  223.         readstring(); /* skip over it */
  224.  
  225.     }
  226.     switch (func) {
  227.         case copy: copystring(); break;
  228.         case expand: xpandstring(delta); break;
  229.         case edit: editstring((struct hshentry *)nil); break;
  230.         case edit_expand: editstring(delta); break;
  231.     }
  232. }
  233.  
  234.     struct cbuf
  235. cleanlogmsg(char *m, size_t s)
  236. {
  237.     register char *t = m;
  238.     register const char *f = t;
  239.     struct cbuf r;
  240.     while (s) {
  241.         --s;
  242.         if ((*t++ = *f++) == '\n')
  243.         while (m < --t)
  244.             if (t[-1]!=' ' && t[-1]!='\t') {
  245.             *t++ = '\n';
  246.             break;
  247.             }
  248.     }
  249.     while (m < t  &&  (t[-1]==' ' || t[-1]=='\t' || t[-1]=='\n'))
  250.         --t;
  251.     r.string = m;
  252.     r.size = t - m;
  253.     return r;
  254. }
  255.  
  256.  
  257. int ttystdin()
  258. {
  259.     static int initialized;
  260.     if (!initialized) {
  261.         if (!interactiveflag)
  262.             interactiveflag = isatty(STDIN_FILENO);
  263.         initialized = true;
  264.     }
  265.     return interactiveflag;
  266. }
  267.  
  268.     int
  269. getcstdin()
  270. {
  271.     register int c = getchar();
  272.     if (c == EOF) {
  273.         if (ferror(stdin))
  274.             IOerror();
  275.         if (ttystdin()) {
  276.             clearerr(stdin);
  277.             afputc('\n',stderr);
  278.         }
  279.     }
  280.     return c;
  281. }
  282.  
  283. #if has_prototypes
  284.     int
  285. yesorno(int default_answer, const char *question, ...)
  286. #else
  287.         /*VARARGS2*/ int
  288.     yesorno(default_answer, question, va_alist)
  289.         int default_answer; const char *question; va_dcl
  290. #endif
  291. {
  292.     va_list args;
  293.     register int c, r;
  294.     if (!quietflag && ttystdin()) {
  295.         oflush();
  296.         vararg_start(args, question);
  297.         fvfprintf(stderr, question, args);
  298.         va_end(args);
  299.         eflush();
  300.         r = c = getcstdin();
  301.         while (c!='\n' && c!=EOF)
  302.             c = getcstdin();
  303.         if (r=='y' || r=='Y')
  304.             return true;
  305.         if (r=='n' || r=='N')
  306.             return false;
  307.     }
  308.     return default_answer;
  309. }
  310.  
  311.  
  312.     void
  313. putdesc(int textflag, const char *textfile)
  314. /* Function: puts the descriptive text into file frewrite.
  315.  * if finptr && !textflag, the text is copied from the old description.
  316.  * Otherwise, if the textfile!=nil, the text is read from that
  317.  * file, or from stdin, if textfile==nil.
  318.  * A textfile with a leading '-' is treated as a string, not a file name.
  319.  * If finptr, the old descriptive text is discarded.
  320.  */
  321. {       register FILE * txt; register int c, old1, old2;
  322.     register FILE * frew;
  323.  
  324.     frew = frewrite;
  325.     if (finptr && !textflag) {
  326.                 /* copy old description */
  327.         aprintf(frew,"\n\n%s%c",Kdesc,nextc);
  328.         foutptr = frewrite;
  329.         getdesc(false);
  330.         } else {
  331.                 /* get new description */
  332.         if (finptr) {
  333.                         /*skip old description*/
  334.             foutptr = NULL;
  335.             getdesc(false);
  336.                 }
  337.         aprintf(frew,"\n\n%s\n%c",Kdesc,SDELIM);
  338.                 if (textfile) {
  339.                         old1='\n';
  340.                         /* copy textfile */
  341.             txt = NULL;
  342.             if (*textfile=='-' || (txt=fopen(textfile,"r"))) {
  343.                 while (txt ? (c=getc(txt))!=EOF : (c = *++textfile)) {
  344.                     if (c==SDELIM) afputc(c,frew); /*double up*/
  345.                     afputc(c,frew);
  346.                                         old1=c;
  347.                                 }
  348.                 if (old1!='\n') afputc('\n',frew);
  349.                 if (txt) ffclose(txt);
  350.                 aprintf(frew, "%c\n", SDELIM);
  351.                 return;
  352.                         } else {
  353.                 efaterror(textfile);
  354.                         }
  355.                 }
  356.                 /* read text from stdin */
  357.         if (feof(stdin))
  358.             faterror("can't reread redirected stdin for description; use -t-<description>");
  359.         if (ttystdin())
  360.             aputs("enter description, terminated with single '.' or end of file:\nNOTE: This is NOT the log message!\n>> ",stderr);
  361.         if ((old1=getcstdin()) != EOF) {
  362.              old2 = '\n';
  363.              for (;;) {
  364.                 if (old1=='\n' && ttystdin())
  365.                     aputs(">> ",stderr);
  366.                 c = getcstdin();
  367.                             if (c==EOF) {
  368.                     afputc(old1,frew);
  369.                     if (old1!='\n') afputc('\n',frew);
  370.                                     break;
  371.                             }
  372.                 if (c=='\n' && old1=='.' && old2=='\n') {
  373.                                     break;
  374.                             }
  375.                 if (old1==SDELIM) afputc(old1,frew); /* double up*/
  376.                 afputc(old1,frew);
  377.                             old2=old1;
  378.                             old1=c;
  379.                     } /* end for */
  380.         }
  381.         aprintf(frew, "%c\n", SDELIM);
  382.         }
  383. }
  384.